#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <list>
#include "../NetEngine_SourceCode/XyRyNet_CommHdr.h"
#include "../NetEngine_SourceCode/XyRyNet_Types.h"
#include "../NetEngine_SourceCode/NetEngine_UPData/UPData_FileParser/FileParser_Define.h"
#include "../NetEngine_SourceCode/NetEngine_UPData/UPData_FileParser/FileParser_Error.h"
#include "../NetEngine_SourceCode/NetEngine_UPData/UPData_Downloads/UPDataDl_Define.h"
#include "../NetEngine_SourceCode/NetEngine_UPData/UPData_Downloads/UPDataDl_Error.h"
#include "../NetEngine_SourceCode/NetEngine_UPData/UPData_HelpModule/HelpModule_Define.h"
#include "../NetEngine_SourceCode/NetEngine_UPData/UPData_HelpModule/HelpModule_Error.h"

//g++ -std=gnu++11 -Wall -g TestFor_UPData.cpp -o TestFor_UPData -L ../NetEngine_Release/NetEngine_Release/NetEngine_PublicLib -L ../NetEngine_Release/NetEngine_Release/NetEngine_Core -L ../NetEngine_Release/NetEngine_Release/NetEngine_UPData -L ../NetEngine_Release/NetEngine_Release/NetEngine_NetHelp -L ../NetEngine_Release/NetEngine_Release/NetEngine_DownLoad -L ../NetEngine_Release/NetEngine_Release/NetEngine_LinuxApi -lNetEngine_BaseLib -lNetEngine_Algorithm -lNetEngine_Core -lNetEngine_ManagePool -lNetEngine_UPDownloads -lNetEngine_UPFileParser -lNetEngine_UPHelpModule -lNetHelp_APIHelp -lNetEngine_Download -lNetEngine_LinuxSdk -Wl,-rpath=../NetEngine_Release/NetEngine_Release/NetEngine_PublicLib:../NetEngine_Release/NetEngine_Release/NetEngine_Core:../NetEngine_Release/NetEngine_Release/NetEngine_UPData:../NetEngine_Release/NetEngine_Release/NetEngine_NetHelp:../NetEngine_Release/NetEngine_Release/NetEngine_DownLoad:../NetEngine_Release/NetEngine_Release/NetEngine_LinuxApi

BOOL bIsRun = TRUE;
void UPData_DownCb(LPCTSTR lpszFileName,double nDownSize,double nFileSize,int nRate,int nAllRate,LPVOID lParam)
{
    if (nRate)
    {
        //printf("文件名：%s,下载大小：%lf，文件大小：%lf，下载进度：%d，下载总进度：%d\n",lpszFileName,nDownSize,nFileSize,nRate,nAllRate);
    }

    if (100 == nAllRate)
    {
        bIsRun = FALSE;
        printf("文件全部下载完成\n");
    }
}
int main()
{
    /*
    LPCTSTR lpszPath = _T("../NetEngine_Release/");
    LPCTSTR lpszFile = _T("./UPList_File.txt");
    LPCTSTR lpszVer = _T("./UPList_Ver.txt");
    LPCTSTR lpszUrl = _T("http://www.xyry.org/UPFile/");

    if (!HelpModule_Api_BuildVer(lpszPath,lpszFile,0,TRUE, lpszVer, lpszUrl))
    {
        printf("文件安装失败，错误：%lX\n",UPHelpModule_GetLastError());
        return 0;
    }
    printf(_T("构建成功\n"));
*/
    int nLen = 8096;
    TCHAR tszJsonMsg[8096];
    TCHAR tszDesp[8096];
    memset(tszJsonMsg,'\0',sizeof(tszJsonMsg));
    memset(tszDesp,'\0',sizeof(tszDesp));

    LPCTSTR lpszUrl = _T("http://www.xyry.org/UPLoad/MainFile_Version.txt");

    if (!FileParser_ReadVer_GetUPVersion(lpszUrl,tszJsonMsg,&nLen))
    {
        printf("获取新版本失败！，错误：%lX\n",UPFileParser_GetLastError());
        return 0;
    }
    int nDesLen = 8096;
    if (!FileParser_ReadVer_GetDescription(tszJsonMsg,tszDesp,&nDesLen))
    {
        printf("获取新版本描述信息失败！，错误：%lX\n",UPFileParser_GetLastError());
        return 0;
    }


    LPCTSTR lpszLocalFile = _T("./LocalFile_List.txt");
    list<FILEPARSER_VERSIONINFO> stl_ListUPDataVer;
    if (!FileParser_Match_Start(lpszLocalFile,tszJsonMsg,&stl_ListUPDataVer))
    {
        printf("匹配版本信息失败！错误：%lX\n",UPFileParser_GetLastError());
        return 0;
    }
    list<FILEPARSER_VERSIONINFO>::const_iterator stl_ListIterator = stl_ListUPDataVer.begin();
    for (;stl_ListIterator != stl_ListUPDataVer.end();stl_ListIterator++)
    {
        if (0 == stl_ListIterator->st_LocalVersion.nModuleVersion)
        {
            printf("增加一个模块，模块名称：%s，模块版本:%ld\n",stl_ListIterator->tszModuleName,stl_ListIterator->nModuleVersion);
        }
        else
        {
            printf("更新一个模块，模块名称：%s，模块版本:%ld\n",stl_ListIterator->tszModuleName,stl_ListIterator->nModuleVersion);
        }
    }

    if (!UPData_DlParser_CBQuery(UPData_DownCb))
    {
        printf("初始化下载器失败%lX\n",UPDataDown_GetLastError());
        return 0;
    }

    LPCTSTR lpszDownPath = _T("./UPFile/");
    if (!UPData_DlParser_Init(lpszDownPath,&stl_ListUPDataVer,TRUE))
    {
        printf("初始化下载器失败%lX\n",UPDataDown_GetLastError());
        return 0;
    }

    while (bIsRun)
    {
        sleep(1);
    }

    if (!UPData_DlParser_Close())
    {
        printf("关闭下载器失败！错误：%lX\n",UPDataDown_GetLastError());
        return 0;
    }
    if (!HelpModule_Api_Install(lpszLocalFile,tszJsonMsg,&stl_ListUPDataVer,lpszDownPath))
    {
        printf("文件安装失败，错误：%lX\n",UPHelpModule_GetLastError());
        return 0;
    }

    return 0;
}
